/*
* @Author: zhangfengjie
* @Date:   2016-04-01 16:57:34
* @Last Modified by:   SongGuangHui
* @Last Modified time: 2017-03-04 14:35:29
*/
/*
 *myScroll 存储实例化的iScroll对象
 * */
var myScroll,pullDownEl, pullDownOffset,pullUpEl, pullUpOffset;
/*---当返回的后台数据全部加载，调用该方法---*/
$(function()
{
    if(total<=firstRow)
    {
        $(".pullUpIcon").hide();
        $(".pullUpLabel").html("没有更多啦");
    }
});
var num = 1; //默认页面显示4条数据              num可变

function pullUpAction (){
    var sourceNode = $(".info_list").eq(0); // 获得被克隆的节点对象
    /* console.log(total+'----'+num*firstRow);*/
    if(total > num*firstRow){
        $.post('/FrontIndex/official_news', {"firstRow":num*firstRow}, function(data, textStatus)
        {
            console.log(data);
            if (data != 'failure' && data != null && data != '')
            {
                var len = data.length;
                var html = '';
                for (var i = 0; i < len; i++)
                {
                    html+='<div class="msg_block">\
                        <a href="/FrontIndex/official_news_detail/article_id/'+data[i].article_id+'">\
                        <h4 class="title">'+data[i].title+'</h4>\
                        <p class="time">'+data[i].add_time+'</p>\
                        <img src="'+data[i].path_img+'?imageMogr2/auto-orient/imageslim" alt="">\
                        </a>\
                    </div>'

              }
                sourceNode.append(html);
                //解决图片高度产生的iscroll.refresh不准确产生的bug
                var imgList = document.getElementsByTagName('img');
                for(var i = 0 ; i < imgList.length ;i++){
                    var img = imgList[i];
                    img.onload = (function(img){
                        return function(){
                            myScroll.refresh();
                            img.onload = null;
                        }
                    })(img);
                }
                setTimeout(function(){
                    myScroll.refresh();
                },500);
                num ++;
            }
        }, 'json');
    }
    else
    {
        $('.pullUpIcon').hide();
        $('.pullUpLabel').html('没有更多了');
    }
}
function loaded() {
  pullUpEl = document.getElementById('pullUp');
  pullUpOffset = pullUpEl.offsetHeight;
//	pullUpAction();
  myScroll = new iScroll('load_wrapper', {
//     useTransition: true,
    topOffset: pullDownOffset,
    onRefresh: function () {
      if (pullUpEl.className.match('loading')) {
        pullUpEl.className = '';
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
      }
    },
    onScrollMove: function () {
       if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
        pullUpEl.className = 'flip';
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '松手开始刷新...';
        this.maxScrollY = this.maxScrollY;
      } else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
        pullUpEl.className = '';
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载...';
        this.maxScrollY = pullUpOffset;
      }
    },
    onScrollEnd: function () {
      if (pullUpEl.className.match('flip')) {
        pullUpEl.className = 'loading';
        pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';
        pullUpAction(); // ajax call?
      }
    }
  });
  setTimeout(function () { document.getElementById('load_wrapper').style.left = '0'; }, 300);
}
//初始化控件
document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 200); }, false);
